const domainName = getApp().globalData.domainName
const parameter = getApp().globalData.parameter
const oss_domain = getApp().globalData.oss_domain
const status_list = getApp().globalData.status_list
const status_bgColors = getApp().globalData.status_bgColors
// pages/students/passing_rate/passing_rate.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status_bgColors,
    status_list,
    this_status_list:[
      {id:'', name:'- 全部 -'},{id:3, name:'在训'},{id:9, name:'初考'},{id:10, name:'补考'}
    ],
    status:'',
    ins_name:'',
    ins_id:'',
    ins_index:0,
    icon_url:oss_domain + 'static/picture/app_icon/',
    loading:true,
    page_no:0,
    page_size:10,
    lis:[]
  },

  update_stuStatus(jg_stu_id, set_status){
    wx.request({
      url: domainName + 'passing_rate_update_stuStatus',
      data: {
        jg_stu_id,
        set_status,
      },...parameter,
      success: res => {
        wx.hideLoading()
        if(res.data.update_res){
          wx.showToast({
            title: '更新完成',
            duration: 1500,
            icon: 'success',
          })

          this.setData({page_no:this.data.page_no-1})
          wx.showLoading()
          this.page_init(this.data.jg_id, 0, this.data.lis.length,this.data.status,this.data.ins_id)
        }
      },
    })
  },

  page_init(jg_id,page_no, page_size, status, ins_id){
    wx.request({
      url: domainName + 'passing_rate_init',
      data: {jg_id,page_no,page_size,status,ins_id},...parameter,
      success: res => {
        wx.hideLoading()
        // console.log('res',res.data)
        let lis = res.data.lis
        this.setData({
          loading:false,
          page_no:this.data.page_no + 1,
          no_more:lis.length==0?true:false,
          lis:page_no==0?lis:this.data.lis.concat(lis),
        })
      },
    })
  },

  // 设置学员等级
  set_stu(e){
    getApp().get_vibration()
    let index = e.currentTarget.id
    console.log('index',index)
    let jg_stu_id = this.data.lis[index].jg_stu_id
    let name = this.data.lis[index].name
    wx.showActionSheet({
      itemList: ['设置为【毕业】','设置为【暂停训练】','设置为【补考】','设置为【初考】','设置为【在训】'],
      alertText: '设置【'+name+'】状态',
      success: res => {
        let tapIndex = res.tapIndex
        var set_status = tapIndex==0?6:tapIndex==1?7:tapIndex==2?10:tapIndex==3?9:tapIndex==4?3:''
        
        wx.showLoading()
        this.update_stuStatus(jg_stu_id,set_status)
      },
    })
  },

  // 查看章节
  view_chap(e){
    let index = e.currentTarget.id
    let jg_stu_id = this.data.lis[index].jg_stu_id
    let name = this.data.lis[index].name
    getApp().get_vibration()
    wx.navigateTo({
      url: '/pages/students/stu_doti_of_chap/stu_doti_of_chap?jg_stu_id=' + jg_stu_id + '&stu_name=' + name,
    })
  },

  // 选择教员
  select_ins(e){
    getApp().get_vibration()
    let index = e.currentTarget.id
    if(index==this.data.ins_index){
      return
    }
    let ins_id = this.data.ins_list[index].ins_id
    this.setData({
      ins_index:index,
      ins_id,
      page_no:0,
    })
    wx.showLoading()
    this.page_init(this.data.jg_id, this.data.page_no, this.data.page_size,this.data.status,this.data.ins_id)
  },


  // 选择学员状态
  select_status(e){
    getApp().get_vibration()

    // console.log('选择学员状态',e)
    let index = e.detail.value
    let status = this.data.this_status_list[index].id
    if(status==this.data.status){
      return
    }
    this.setData({
      status,
      page_no:0,
    })
    wx.showLoading()
    this.page_init(this.data.jg_id, this.data.page_no, this.data.page_size,this.data.status,this.data.ins_id)
  },

  passing_rate_loadInsList(jg_id){
    wx.request({
      url: domainName + 'passing_rate_loadInsList',
      data: {jg_id},...parameter,
      success: res => {
        this.setData({
          ins_list:res.data.ins_list
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.showLoading()
    // console.log('options',options)
    this.setData({
      jg_id: options.jg_id
    })
    wx.showLoading()
    this.page_init(this.data.jg_id, this.data.page_no, this.data.page_size,this.data.status,this.data.ins_id)
    this.passing_rate_loadInsList(this.data.jg_id)
  },

  onReachBottom(){
    if(!this.data.no_more){
      wx.showLoading()
      this.page_init(this.data.jg_id, this.data.page_no, this.data.page_size,this.data.status,this.data.ins_id)
    }
  },

})